﻿//Copyright 2011 University of Southampton 
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.UserCode;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;



namespace UniversityOfSouthampton.Taxonomy
{
    #region InputObject
    [Serializable]
    public class TaxonomyCreateGroupArgs : SPProxyOperationArgs
    {
        // Fields...
        private string _Description;
        private string _Name;
       

        /// <summary>
        /// Group description
        /// </summary>
        public string Description
        {
            get { return _Description; }
            set
            {
                _Description = value;
            }
        }

        private string _URL;
        public string URL
        {
            get { return _URL; }
            set
            {
                _URL = value;
            }
        }
         
        /// <summary>
        /// Group Name
        /// </summary>
        public string Name
        {
            get { return _Name; }
            set
            {
                _Name = value;
            }
        }
        
        public TaxonomyCreateGroupArgs()
        {
            
        }
    }
    
    #endregion
    [Serializable]
    #region Output object
    public class TaxonomyCreateGroupResult : SPProxyOperationArgs
    {
        // Fields...
        private Guid _Id;

        // Fields...
        private string _Description;
        private string _Name;

        /// <summary>
        /// Group description
        /// </summary>
        public string Description
        {
            get { return _Description; }
            set
            {
                _Description = value;
            }
        }

        /// <summary>
        /// Group Name
        /// </summary>
        public string Name
        {
            get { return _Name; }
            set
            {
                _Name = value;
            }
        }

        public Guid Id
        {
            get { return _Id; }
            set
            {
                _Id = value;
            }
        }
        

        public TaxonomyCreateGroupResult()
        {
            
        }
    }
    
    #endregion

    [Microsoft.SharePoint.Security.SharePointPermission(System.Security.Permissions.SecurityAction.LinkDemand, ObjectModel = true)]
    public class TaxonomyCreateGroup : SPProxyOperation
    {
        #region Properties 
        public static string ProxOperationTypeName
        {
            get
            {
                return UniversityOfSouthampton.Taxonomy.Properties.Resources.CreateGroupClassName;
            }
        }

        public static string ProxyAssemblyName
        {
            get
            {
                return UniversityOfSouthampton.Taxonomy.Properties.Resources.UniversityOfSouthamptonTaxonomySharepointDLLName;
            }
        }
        #endregion


        public override object Execute(SPProxyOperationArgs args)
        {
            TaxonomyCreateGroupResult results = new TaxonomyCreateGroupResult();
            if (args != null)
            {
                TaxonomyCreateGroupArgs inputData = args as TaxonomyCreateGroupArgs;
               //string sharepointSiteURL = SPWebApplication.Lookup(new Uri(inputData.URL));
                using (TaxonomyManagementFullTrustLib taxLib = new TaxonomyManagementFullTrustLib(inputData.URL))
                {
                    //Transfer object data (dont pass to core lib)
                    

                    Group grp =  taxLib.CreateNewTermStoreGroup(inputData.Name, inputData.Description);

                    results.Id = grp.Id;
                    results.Name = grp.Name;
                    results.Description = grp.Description;

                }
            }
            return results;
        }
    }
}
